define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var Model = function(){
	    this.loadDone1 = false;
	    this.loadDone2 = false;
	    this.loadDone3 = false;
		this.callParent();
		this.isInSearch = false;
	};

	Model.prototype.shop1DataCustomRefresh = function(event){
	        
		this.getAndLoadShops(1, event);
	};

	Model.prototype.shop2DataCustomRefresh = function(event){
		this.getAndLoadShops(2, event);
	};

	Model.prototype.shop3DataCustomRefresh = function(event){
		this.getAndLoadShops(3, event);
	};
	
	
	//ajax获取商店列表并加载到data组件
	Model.prototype.getAndLoadShops = function(type,event){
	    if(event.source.count() < event.offset){
	    	return;
	    }	
	    this.comp("popOver1").show();
	    var me = this;
		var url = this.getParentModel().serverUrl + "/ex_community/a/api/community/getBusiness?callback=?";
		var page  = event.offset/event.limit +  1;
		$.getJSON(url,
		        {
		        	businessType:type,
		        	pageSize:event.limit,
		        	pageNo:page
		        },
				function(data){
					if(data && data.data){
						event.source.loadData(data.data,true,null,event.offset,true);	
						if(type == 1){
							me.loadDone1 = true;
						}else if(type == 2){
							me.loadDone2 = true;
						}else{
							me.loadDone3 = true;
						}
						
						me.comp("popOver1").hide();
										
					}
				}							
		);
	};

	Model.prototype.list1Click = function(event){
	    var shopType = this.comp("shop1Data").getCurrentRow().val("id");
		justep.Shell.showPage("$UI/communityOto/home/goodsList.w?shopType="+shopType);
	};
	
	Model.prototype.changeActiveStyle = function(event){
		$(".btn-group .btn-link").removeClass("newTypeBorder");
		$(event.currentTarget).addClass("newTypeBorder");
		
	};

	

	Model.prototype.backBtnClick = function(event){
	
	    if(this.isInSearch){
	    	var shopType = this.comp("contents1").getActiveIndex()+1;
	    	this.refreshShopDataByType(shopType);
	    	this.isInSearch = false;
	    	this.comp("searchContent").val('');
	    }else{
	    	this.comp("windowReceiver1").windowEnsure();
		    this.getParentModel().goBackHome();
	    }
		

	};

	

	Model.prototype.list2Click = function(event){
		var shopType = this.comp("shop2Data").getCurrentRow().val("id");
		justep.Shell.showPage("$UI/communityOto/home/goodsList.w?shopType="+shopType);
	};

	

	Model.prototype.list3Click = function(event){
		var shopType = this.comp("shop3Data").getCurrentRow().val("id");
		justep.Shell.showPage("$UI/communityOto/home/goodsList.w?shopType="+shopType);
	};
	
	Model.prototype.showSearchButtonClick = function(event){
	
		$('.search-panel').slideToggle();

	};
		
	Model.prototype.searchButtonClick = function(event){
	   
		var searchUrl = this.getParentModel().serverUrl + "/ex_community/a/api/community/getBusiness?callback=?";
		var title = this.comp("searchContent").val();
		var shopType = this.comp("contents1").getActiveIndex()+1;
		var me  = this;
		if(title){
			$.getJSON(searchUrl,{'businessType':shopType,'name':title},function(data){
				if(data && data.data){
				    me.isInSearch = true;
					if(shopType === 1){
						me.comp("shop1Data").loadData(data.data);
					}else if(shopType === 2){
						me.comp("shop2Data").loadData(data.data);
					}else{
						me.comp("shop2Data").loadData(data.data);
					}
				}
			});
		}
	};
		

	Model.prototype.contents1ActiveChange = function(event){
		if(this.isInSearch){
	    	var shopType = event.from + 1;
	    	this.refreshShopDataByType(shopType);
	    }
	    this.comp("searchContent").val('');
		this.isInSearch = false;//切换商城类型后要重新搜索，返回按钮直接返回上一页，
	};
	
	
	Model.prototype.refreshShopDataByType = function(shopType){
		if(shopType === 1){
	    		this.comp("shop1Data").refreshData();
	    	}else if(shopType == 2){
	    		this.comp("shop2Data").refreshData();
	    	}else{
	    		this.comp("shop3Data").refreshData();
	    	}
	};
		
	
	

	Model.prototype.scrollView1PullDown = function(event){
		this.comp("shop1Data").refreshData();
	};
		
	
	

	Model.prototype.scrollView1PullUp = function(event){
	    if(this.loadDone1){
	        this.loadDone1 = false;
	    	this.comp("shop1Data").loadNextPageData();
	    }
		
	};
		
	
	

	Model.prototype.scrollView2PullDown = function(event){
		this.comp("shop2Data").refreshData();
	};
		
	
	

	Model.prototype.scrollView2PullUp = function(event){
	    if(this.loadDone2){
	        this.loadDone2 = false;
	    	this.comp("shop2Data").loadNextPageData();
	    	
	    }
		
	};
		
	
	

	Model.prototype.scrollView3PullDown = function(event){
		this.comp("shop3Data").refreshData();
	};
		
	
	

	Model.prototype.scrollView3PullUp = function(event){
	    if(this.loadDone3){
	    	this.loadDone3 = false;
	    	this.comp("shop3Data").loadNextPageData();
	    }
		
	};
		
	
	

	Model.prototype.shop1DataBeforeRefresh = function(event){
		this.loadDone1 = false;
	};
		
	
	

	Model.prototype.shop2DataBeforeRefresh = function(event){
		this.loadDone2 = false;
	};
		
	
	

	Model.prototype.shop3DataBeforeRefresh = function(event){
		this.loadDone3 = false;
	};
		
	
	

	return Model;
});